package com.zby.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.dto.ReturnRecordsDto;
import com.zby.admin.service.ReturnRecordsService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/returnRecords")
public class ReturnRecordsController {
    @Resource
    private ReturnRecordsService returnRecordsService;
    /**
     * 添加退货记录
     */
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody ReturnRecordsDto  returnRecordsDto, HttpServletRequest request) {
       boolean insert = returnRecordsService.insertReturnRecords(returnRecordsDto, request);
        return Result.success();
    }
    /**
     * 获取所有退货记录（分页）
     */
    @GetMapping("/getAllReturnRecords")
    public Result<Page<ReturnRecordsDto>>getAllReturnRecords( @RequestParam Integer pageNum, @RequestParam Integer pageSize,HttpServletRequest request) {
        // 判断是否为管理员
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }

        Page<ReturnRecordsDto> pageResult =  returnRecordsService.getAllReturnRecords(pageNum, pageSize);
        return Result.success(pageResult);
    }
    
    /**
     * 获取所有退货记录（列表）
     */
    @GetMapping("/getAllReturnRecordsList")
    public Result<List<ReturnRecordsDto>> getAllReturnRecordsList(HttpServletRequest request) {
        // 判断是否为管理员
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }

        List<ReturnRecordsDto> listResult =  returnRecordsService.getAllReturnRecordsList();
        return Result.success(listResult);
    }
    /**
     * 根据单号获取退货记录
     */
    @GetMapping("/getReturnRecordsByCode")
    public Result<ReturnRecordsDto> getReturnRecordsByCode(@RequestParam Long code) {
        ReturnRecordsDto records =   returnRecordsService.getReturnRecordsByCode(code);
        return Result.success(records);
    }
}